<!--
  * 分账流水
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-col :span="3">
        <a-form-item label="交易订单号" class="smart-query-form-item">
          <a-input v-model:value="queryForm.no" placeholder="输入交易订单号" />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="交易日期" class="smart-query-form-item">
          <a-date-picker :allowClear="false" :inputReadOnly="true" v-model:value="queryForm.fTime" />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="支付通道" class="smart-query-form-item">
          <CustomSelect
            v-model:value="queryForm.channelName"
            :placeholder="'请先选择支付通道'"
            :api="channelApi.queryPage"
            value-field="channelName"
            label-field="channelName"
          />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="是否退款" class="smart-query-form-item">
          <a-select v-model:value="queryForm.type" allowClear :options="SERVICE_TYPE" placeholder="请选择是否退款" />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="分账类型" class="smart-query-form-item">
          <a-select v-model:value="queryForm.settleType" allowClear :options="SETTLE_TYPE" placeholder="请选择分账类型" />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="交易状态" class="smart-query-form-item">
          <a-select v-model:value="queryForm.settleType" allowClear :options="SETTLE_TYPE" placeholder="请选择交易状态" />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="分账状态" class="smart-query-form-item">
          <a-select v-model:value="queryForm.settleType" allowClear :options="SETTLE_TYPE" placeholder="请选择分账状态" />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="商户名称" class="smart-query-form-item">
          <a-input v-model:value="queryForm.no" placeholder="输入商户名称" />
        </a-form-item>
      </a-col>
      <!-- 
      <a-form-item label="状态" class="smart-query-form-item">
        <SmartEnumSelect width="200px" v-model:value="queryForm.status" enum-name="STATUS_ENUM" placeholder="状态" />
      </a-form-item> 
      -->
      <a-col :span="3">
        <a-form-item class="smart-query-form-item">
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-form-item></a-col
      >
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-title">数据列表</div>
      <!-- <div class="smart-table-operate-block">
      </div> -->
      <div class="smart-table-setting-block">
        <a-button @click="showForm" type="primary" size="small" v-privilege="'trade:insert'">
          <template #icon>
            <PlusOutlined />
          </template>
          新增
        </a-button>
        <!-- <TableOperator v-model="columns" :tableId="null" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ record, column }">
        <!-- <template v-if="column.dataIndex === 'status'">
          <a-switch
            v-privilege="'trade:update_status'"
            :checked="text"
            :checkedValue="1"
            :unCheckedValue="0"
            @change="(checked) => handleStatusChange(checked, record)"
            :loading="record.statusLoading"
          />
        </template> -->
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="view(record)" type="link">分账明细</a-button
            ><!--  v-privilege="'trade:update'" -->
            <a-button @click="viewForm(record)" type="link">详情</a-button
            ><!--  v-privilege="'trade:update'" -->
          </div>
        </template>
      </template>
    </a-table>

    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :page_sizeOptions="PAGE_SIZE_OPTIONS"
        :defaultpage_size="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>

  <a-modal title="详情" :width="800" :open="openDialog" @cancel="onClose" :maskClosable="false" :destroyOnClose="true" :footer="false">
    <div>
      <a-descriptions :column="2">
        <a-descriptions-item :label="item.label" v-for="(item, index) in detailConfig" v-bind="item.attrOpt" :key="index">{{
          item.customRender ? item.customRender({ config: item, text: formData[item.key] }) : formData[item.key]
        }}</a-descriptions-item>
      </a-descriptions>
    </div>
  </a-modal>

  <CeparateAccountDetails ref="separateAccountDetails" @cancel="() => (openDetail = false)" :open="openDetail" />
</template>
<script setup>
  import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
  import { message, Modal } from 'ant-design-vue'
  import { separateAccountFlowPage, chargeDelete, chargeUpdateStatus } from '/@/api/business/b-operation/channel/charge-api.js'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import CustomSelect from '/@/components/business/comment/CustomSelect.vue'
  import CeparateAccountDetails from './separate-account-details.vue'
  import { channelApi } from '/@/api/business/b-operation/channel/channel-api'
  import dayjs from 'dayjs'
  // import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue'
  import { selectDictLabel, formatToMoney } from '/@/utils/common'
  import {
    BILLING_TYPE, //计费类型
    CHARGE_USER_TYPE, //	计费对象
    CHARGE_TYPE, //计费模式
    SETTLE_TYPE, //'结算模式'
    SERVICE_TYPE, //'服务类型'
  } from '/@/constants/dict'
  import { isEmpty } from 'lodash'
  import { cloneDeep } from 'lodash-es'

  const { proxy } = getCurrentInstance()

  // ---------------------------- 表格列 ----------------------------
  const columns = ref([
    {
      title: '序号',
      width: 50,
      align: 'center',
      dataIndex: 'index',
      customRender: ({ index }) => {
        return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
      },
    },
    {
      title: '交易订单号',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '交易时间',
      dataIndex: 'cTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      ellipsis: true,
    },
    {
      title: '支付通道',
      dataIndex: 'type',
      customRender: ({ text }) => {
        return selectDictLabel(BILLING_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '商户名称',
      dataIndex: 'title',
      ellipsis: true,
    },
    {
      title: '分账出账户',
      dataIndex: 'settleType',
      customRender: ({ text }) => {
        return selectDictLabel(SETTLE_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '分账类型',
      dataIndex: 'chargeType',
      customRender: ({ text }) => {
        return selectDictLabel(CHARGE_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '分账状态',
      dataIndex: 'chargeUserType',
      customRender: ({ text }) => {
        return selectDictLabel(CHARGE_USER_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '交易金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '分账金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '服务费',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '实收金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '是否退款',
      dataIndex: 'chargeUserType',
      customRender: ({ text }) => {
        return selectDictLabel(CHARGE_USER_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '退款金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '交易状态',
      dataIndex: 'chargeUserType',
      customRender: ({ text }) => {
        return selectDictLabel(CHARGE_USER_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '交易失败原因',
      dataIndex: 'status',
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 80,
    },
  ])
  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    type: undefined, //计费类型
    settleType: undefined, //结算类型
    chargeType: undefined, //计费模式
    chargeUserType: undefined, //计费用户类型
    pageNo: 1,
    pageSize: 10,
  }

  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await separateAccountFlowPage(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }

  onMounted(queryData)

  // ---------------------------- 添加/修改 ----------------------------
  const formRef = ref()
  const openForm = ref(false)
  const chargeFormType = ref()
  const chargeFormId = ref()

  function showForm(row) {
    openForm.value = true
    chargeFormType.value = isEmpty(row) ? 'add' : 'update'
    chargeFormId.value = isEmpty(row) ? '' : row.id
  }
  // 查明细
  const openDetail = ref(false)
  function view(row) {
    openDetail.value = true
  }

  // 详情
  const infoForm = { merchantName: 1 }
  const detailConfig = [
    { label: '通道名称', key: 'merchantName', attrOpt: {} },
    { label: '公司名称', key: 'merchantNo' },
    { label: '商户名称', key: 'merchantNo' },
    { label: '商户通道ID', key: 'merchantType' },
    { label: '交易订单号', key: 'merchantContact' },
    { label: '账户交易号', key: 'merchantContactMobile' },
    { label: '结算交易号', key: 'merchantContactMobile' },
    { label: '结算模式', key: 'merchantContactMobile' },
    { label: '付款人ID', key: 'merchantContactMobile' },
    { label: '付款账号', key: 'merchantContactMobile' },
    {
      label: '订单总金额',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '资金冻结',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '订单标题',
      key: 'merchantContactMobile',
    },
    {
      label: '服务费',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '已通知',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '通知时间',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '是否退款',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '失败原因信息',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '状态',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '结算时间',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '付款时间',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '成功通知时间',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
    {
      label: '创建时间',
      key: 'merchantContactMobile',
      customRender: (column) => formatToMoney(column.text / 100),
    },
  ]

  const formData = ref({ ...cloneDeep(infoForm) })
  const openDialog = ref(false)
  function viewForm(row) {
    openDialog.value = true
  }

  function onClose() {
    formData.value = cloneDeep(infoForm)
    openDialog.value = false
  }
</script>
